<template>
  <div class="employees">
    <el-card class="btn">
      <el-button type="primary" @click="goImport">导入</el-button>
      <el-button type="primary" @click="add">+新增员工</el-button>
    </el-card>
    <el-card>
      <el-table :data="list" style="width: 100%">
        <el-table-column
          label="序号"
        ><template v-slot="{ $index }">
          <div>
            {{ (page.page - 1) * page.size + $index + 1 }}
          </div>
        </template></el-table-column>
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="staffPhoto" label="头像">
          <template v-slot="{ row }">
            <img
              v-imgerror="errorUrl"
              :src="row.staffPhoto"
              alt=""
              class="avatar"
              @click="imgClick(row.staffPhoto)"
            >
          </template>
        </el-table-column>

        <el-table-column prop="mobile" label="手机号" />
        <!-- :formatter="formatter" -->
        <el-table-column prop="formOfEmployment" label="聘用形势">
          <!-- <template v-slot="{ row }">
            <div v-hrieType="row.formEmployment">
              {{ row.formEmployment | formatType }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column prop="timeOfEntry" label="入职时间">
          <!-- <template v-slot="{ row }">
            <div>
              {{ row.timeOfEntry | formtTime }}
            </div>
          </template> -->
        </el-table-column>
        <el-table-column prop="enableState" label="状态" />
        <el-table-column label="操作" width="300" fixed="right">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text">角色</el-button>
              <el-button type="text" @click="del(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          layout="sizes,total,prev, pager, next"
          :total="page.total"
          :page-sizes="[1, 5, 10, 20, 200, 300, 400]"
          :page-size="page.size"
          :current-page="page.page"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>
    <Dialog :is-show.sync="show" @getDate="getData" />
    <el-dialog title="二维码" :visible.sync="showQrcode">
      <div style="text-align: center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { sysUser, sysUserDel } from '@/api/employees'
import employees from '@/assets/constant/employees'
import Dialog from './components/dialog.vue'
import QrCode from 'qrcode'
export default {
  components: {
    Dialog
  },
  data() {
    return {
      showQrcode: false,
      errorUrl: require('@/assets/common/head.jpg'),
      show: false,
      page: {
        total: 100,
        page: 1,
        size: 1
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    imgClick(imgUrl) {
      this.showQrcode = true
      this.$nextTick(() => {
        QrCode.toCanvas(this.$refs.canvas, imgUrl, {
          width: 300,
          height: 300
        })
      })
    },
    goImport() {
      this.$router.push(
        '/employees/import?name=' +
          encodeURIComponent('员工') +
          `&redirect=${encodeURIComponent('/employees')}`
      )
    },
    add() {
      this.show = true
    },
    del(id) {
      this.$confirm('确认删除吗?', '提示')
        .then(async() => {
          await sysUserDel(id)
          this.$message.success('删除成功')
          // 回到第一页
          this.handleCurrentChange()
        })
        .catch(() => {})
    },
    async getData() {
      const res = await sysUser(this.page)

      res.data.rows.forEach((item) => {
        // console.log(item)
        item.formOfEmployment = this.formatter(
          null,
          null,
          item.formOfEmployment
        )
      })
      // console.log('员工列表', res)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    handleSizeChange(size) {
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    formatter(row, column, cellValue, index) {
      const result = employees.hireType.find((item) => {
        // console.log(item)
        return item.id === cellValue
      })
      // console.log(cellValue)
      return result ? result.value : '-'
    },
    goDetail(id) {
      this.$router.push('/employees/details/' + id)
    }
  }
}
</script>
<style lang="scss" scoped>
.avatar {
  width: 50px;
  height: 50px;
}
</style>
